<template>
	<f-popup ref="popup" position="bottom">
		<view class="back-order">
			<view class="head-title">确认归还</view>
			<scroll-view scroll-y class="scroll" style="height: 54vh;">
			
				<view class="card">
					<view class="card-title">产品信息</view>
					<view class="title">
						{{info.product_title}}
						<view v-if="info.product_is_sn" class="info-item">SN：{{info.product_sn}}</view>
					</view>
					<!-- <view class="info-list">
						<view class="info-item">租赁方式：
							<view class="status">
								<view v-if="info.is_direct === 0" class="status-item status-wait">向服务商租赁</view>
								<view v-else class="status-item status-wait">向贝贝享飞平台租赁</view>
							</view>
						</view>
					</view> -->
				</view>
				<view class="card">
					<view class="form">
						<view class="form-item">
							<view class="form-label">拍照留档<text style="color: red;">*</text></view>
							<view class="form-body" style="border: none;padding-bottom: 0;">
								<upload-img ref="uploadImg" :value="formData.images" multiple module="order" @success="handleUpload($event, 'images')" @remove="handleRemove($event, 'images')"></upload-img>
							</view>
						</view>
						<view class="form-item">
							<view class="form-label">备注说明</view>
							<view class="form-body" style="border-bottom: none;">
								<view class="textarea">
									<textarea v-model="formData.remark" placeholder="请输入备注说明"
										placeholder-class="placeholder-class"></textarea>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>

			<view class="footer-btns">
				<button class="confirm-btn flex-1" @click="handleConfirm">确认</button>
			</view>
		</view>
	</f-popup>

</template>

<script>
	const checker = require("@/utils/checker.js");
	
	export default {
		name: 'back-order',
		data() {
			return {
				formData: {
					images: [], // 图片组，多个图片，逗号分隔
					remark: '', // 备注描述 可选
				},
				rule: [
					{ name: "images",checkType: "notnull",checkRule: "",errorMsg: "请上传交付图片" },
				],
				address: {},
				info: {}
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			// 前往库存列表
			goStockList() {
				uni.navigateTo({
					url: '/pages/agent/stock-list/stock-list'
				})
			},
			handleConfirm() {
				const formData = { ...this.formData }
				formData.images = formData.images.map(item=> item.url).join(',')
				const checkRes = checker.check(formData, this.rule);
				if (checkRes) {
					this.handleSubmit(formData)
				} else {
					uni.showToast({
						title: checker.error,
						icon: "none"
					});
				}
			},
			handleSubmit(formData) {
				console.log('info', this.info);
				this.$api.agentUserRentOrderComfirmBackApi(this.info.gid, formData).then(res=> {
					uni.showToast({
						title: '归还成功'
					})
					this.$emit('refresh')
					this.close()
				}).catch(err=> {
					console.log('err', err);
				})
			},
			// 上传图片
			handleUpload(files, key) {
				this.formData[key] = files.map(item=> ({url: item.url}))
			},
			// 删除图片
			handleRemove({files}, key) {
				this.formData[key] = files.map(item=> ({url: item.url}))
			},
			bindDateChange(e) {
				const { value } = e.detail
				this.remark.date = value
			},
			open(info) {
				this.info = info
				this.formData = {
					images: [], // 图片组，多个图片，逗号分隔
					remark: '', // 备注描述 可选
				}
				this.$refs.popup.open()
			},
			close() {
				this.$refs.popup.close()
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.back-order {
		width: 100%;
		padding: 0 32rpx;
		box-sizing: border-box;
		background-color: #F6F6F6;
		border-top-left-radius: 26rpx;
		border-top-right-radius: 26rpx;

		.head-title {
			font-size: 34rpx;
			font-weight: 600;
			line-height: 46rpx;
			text-align: center;
			color: #000000;
			padding: 32rpx;
		}

		.scroll {
			padding-bottom: calc(120rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
			padding-bottom: calc(120rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		}

		.card {
			padding: 38rpx 32rpx;
			border-radius: 16rpx;
			margin-bottom: 32rpx;
			box-sizing: border-box;
			background-color: #FFFFFF;
			box-shadow: 0px 0px 33rpx rgba(0, 0, 0, 0.05);
		}

		.card-title {
			font-size: 28rpx;
			font-weight: 400;
			text-align: left;
			line-height: 44rpx;
			padding-bottom: 8rpx;
			color: rgba(0, 0, 0, 0.9);
		}

		.title {
			color: #000000;
			font-size: 34rpx;
			font-weight: 400;
			text-align: left;
			line-height: 46rpx;
		}

		.info-list {
			gap: 16rpx;
			display: flex;
			padding-top: 38rpx;
			// padding-bottom: 38rpx;
			flex-direction: column;
		}

		.info-item {
			color: #999999;
			font-size: 26rpx;
			text-align: left;
			font-weight: 400;
			line-height: 36rpx;

			display: flex;
			align-items: center;
		}

		.form {

			.form-item {
				padding-bottom: 32rpx;

				&:last-child {
					padding-bottom: 0;
				}
			}

			.form-label {
				font-size: 28rpx;
				font-weight: 400;
				text-align: left;
				line-height: 44rpx;
				padding-bottom: 8rpx;
				color: rgba(0, 0, 0, 0.9);
			}

			.form-body {
				padding-bottom: 12rpx;
				border-bottom: 1px solid #E7E7E7;
			}

			.input {
				width: 100%;
				height: 72rpx;
				display: flex;
				align-items: center;

				input {
					flex: 1;
					height: 100%;
				}
			}

			.picker {
				width: 100%;
				height: 72rpx;
				display: flex;
				align-items: center;
				
				picker {
					width: 100%;
					height: 72rpx;
				}
				.picker-body {
					width: 100%;
					height: 72rpx;
					display: flex;
					align-items: center;
					box-sizing: border-box;
				}
			}

			.hint {
				font-size: 24rpx;
				font-weight: 400;
				text-align: left;
				line-height: 40rpx;
				padding-bottom: 10rpx;
				color: rgba(0, 0, 0, 0.4);
			}

			.right-btn {
				position: relative;
				padding: 0 32rpx;
				height: 48rpx;
				color: var(--primary-color);
				font-size: 32rpx;
				font-weight: 400;
				line-height: 48rpx;
				text-align: center;
				line-height: 48rpx;

				&:before {
					position: absolute;
					content: '';
					top: 5%;
					left: 0;
					bottom: 0;
					width: 1px;
					height: 90%;
					background-color: #E7E7E7;
				}
			}

			.textarea {
				height: 168rpx;
				padding: 14rpx 16rpx;
				border-radius: 12rpx;
				box-sizing: border-box;
				border: 1px solid #DCDCDC;

				textarea {
					width: 100%;
					height: 100%;
					font-size: 32rpx;
					font-weight: 400;
					line-height: 48rpx;
					text-align: left;
				}
			}

		}



		.footer-btns {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			width: 100%;
			z-index: 9999;
			padding: 20rpx 32rpx;
			box-sizing: border-box;
			background-color: #FFFFFF;
			box-shadow: 0px 0px 33rpx rgba(0, 0, 0, 0.05);
			padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
			padding-bottom: calc(20rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/

			gap: 14rpx;
			display: flex;
			align-items: center;
		}
	}
</style>